﻿public class NetHandler_GetUserMomentsInfos : GameServerMessageHandler
{
    public const string Key = "GetUserMomentsInfo";

    public static NetHandler_GetUserMomentsInfos NewHandler()
    {
        return new NetHandler_GetUserMomentsInfos();
    }

    protected override ISerialize NewData() => new GetUserMomentsInfoRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        GetUserMomentsInfoRequestData requestData = _data as GetUserMomentsInfoRequestData;

        UserServer user = Game.instance.GetUser(client.userID);

        GetUserMomentsInfoResponseData response = new GetUserMomentsInfoResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                UserMomentsInfo res = new UserMomentsInfo();

                res.user = UserServer.GetUserInfoWithUserId(requestData.userId);
                res.moments = user.data.moments.Map(e=> new UserMoment(res.user, e)).AsArray();
                response.userMomentsInfo = res;
                response.code = 0;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        GameDebug.Log($"用户{response.userMomentsInfo?.user?.userId} 获取MomentsInfos  moments length:{response.userMomentsInfo?.moments?.Length}");
        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
